package ua.exchangeratesukraine.ui.activity;

import ua.exchangeratesukraine.R;
import ua.exchangeratesukraine.ui.fragment.FragmentBase;
import ua.exchangeratesukraine.ui.fragment.FragmentMenu;
import ua.exchangeratesukraine.ui.fragment.SetActionListener;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity implements SetActionListener {

	private final String		LOG_TAG		= getClass().getSimpleName();
	private SlidingMenu			slidingMenu	= null;
	private boolean				isDualPane	= false;
	public SharedPreferences	prefs		= null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		setBehindContentView(R.layout.activity_menu);

		if (((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL)
				|| ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL)) {

			getSupportActionBar().setDisplayShowTitleEnabled(false);
			getSupportActionBar().setCustomView(LayoutInflater.from(this).inflate(R.layout.action_bar_view, null));
			getSupportActionBar().setDisplayShowCustomEnabled(true);
		}

		slidingMenu = getSlidingMenu();
		isDualPane = (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);

		if (isDualPane) {

			FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
			Fragment fragmentMenu = new FragmentMenu();
			ft.add(R.id.layoutMenu, fragmentMenu);
			ft.commit();
			slidingMenu.setBehindOffset(getDisplayWidth());
		} else {

			slidingMenu.setBehindOffset(getDisplayWidth() * 3 / 4);
		}
		setSlidingActionBarEnabled(false);
        prefs = getSharedPreferences("ua.exchangeratesukraine", MODE_PRIVATE);
	}

	protected int getDisplayWidth() {
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		Log.d(LOG_TAG, "resolution: " + metrics.widthPixels + " x " + metrics.heightPixels);
		return metrics.widthPixels;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (isDualPane) return false;

		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.show_menu:
				showMenu();
				return true;
			case R.id.hide_menu:
				showContent();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		showMenu();
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public void setAction() {
		showContent();
	}

	public void sortActivity(View view) {
		FragmentBase fragment = (FragmentBase)getSupportFragmentManager().findFragmentById(R.id.fragmentLayout);
		fragment.sortFragment(view, false);
	}

	AlertDialog dlgQuit = null;

	protected void quit() {
		
		getIntent().putExtra(getResources().getString(R.string.quit_key), true);
		
		final Context context = this.getApplicationContext();
		AlertDialog.Builder dlgQuitBuilder = new AlertDialog.Builder(this);
		dlgQuitBuilder.setMessage(getResources().getString(R.string.confirmation_To));
		dlgQuitBuilder.setTitle(getResources().getString(R.string.quit_app));
		dlgQuitBuilder.setCancelable(false);

		dlgQuitBuilder.setPositiveButton(getResources().getString(R.string.Yes), new OnClickListener() {
			public void onClick(DialogInterface dialog, int arg) {
				Toast.makeText(context, getResources().getString(R.string.Thank), Toast.LENGTH_LONG).show();
				finish();
			}
		});
		dlgQuitBuilder.setNegativeButton(getResources().getString(R.string.No), new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				getIntent().putExtra(getResources().getString(R.string.quit_key), false);
				closeQuitDialog();
			}
		});
		dlgQuit = dlgQuitBuilder.create();
		dlgQuit.show();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (getIntent().getBooleanExtra(getString(R.string.finish_key), false)) {
			finish();
		}
	}

	protected void closeQuitDialog() {
		if (dlgQuit != null && dlgQuit.isShowing())
			dlgQuit.dismiss();
	}

	@Override
	protected void onStop() {
		super.onStop();
		closeQuitDialog();
	}

	@Override
	public void onBackPressed() {
		quit();
	}
}